import { reqGoodsInfo, reqAddUpdateShopCatr } from '@/api'
import {getUUID} from "@/utils/uuid_token"

const actions = {
  async getGoodsInfo({ commit }, skuId) {
    let result = await reqGoodsInfo(skuId)
    if (result.code == 200) {
      commit('GETGOODINFO', result.data)
    }
  },
  // 购物车数量
  async addOrUpdateShopCart({ commit }, { skuId, skuNum }) {
    let result = await reqAddUpdateShopCatr(skuId, skuNum)
    if (result.code == 200) {
      return 'ok'
    } else {
      return Promise.reject(new Error('faile'))
    }
  }
}
const mutations = {
  GETGOODINFO(state, goodInfo) {
    state.goodInfo = goodInfo

  }
}

const state = {
  goodInfo: [],
  uuid_token: getUUID()

}

const getters = {
  categoryView(state) {
    return state.goodInfo.categoryView || {}
  },
  skuInfo(state) {
    return state.goodInfo.skuInfo || {}
  },
  spuSaleAttrList(state) {
    return state.goodInfo.spuSaleAttrList || []
  }

}

export default {
  namespaced: true,
  actions,
  mutations,
  state,
  getters
}